home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / More ResEdit Preferences / Source Code / ResINIT.c < prev    next >
Encoding:
Text File  |  1996-06-22  |  4.5 KB  |  210 lines  |  [TEXT/KAHL]

  1. // ResEdit Extensions
  2. // Copyright 1995#include <A4Stuff.h>
  3. #include "ShowINIT.h"
  4. #include <Folders.h>
  5. #include <Script.h>
  6. #include <Aliases.h>
  7. #include <Processes.h>
  8. #include <GestaltEqu.h>
  9. #include <Traps.h>
  10.  
  11. #define        kGoodIcon        128
  12. #define        kBadIcon        129
  13.  
  14. #define        kMaxRecursion    10
  15. #define        kFolderName        "\pMore ResEdit Preferences"
  16.  
  17. long        addrStorage;
  18.  
  19. void asm patchEntry (void);
  20. OSErr getMyPrefsFolder (FSSpec *myPrefs);
  21. void myPatch (Str255 fileName);
  22. void main (void);
  23.  
  24. // Open all resource files in the folder
  25. // Open sub-folders and aliases to sub folders
  26.  
  27. static
  28. void openAllInFolder (FSSpec *folder, short level)
  29.  
  30. {
  31.     CInfoPBRec        pBlock;
  32.     OSErr            error;
  33.     long            dirID;
  34.     Str63            name;
  35.     FSSpec            spec;
  36.     Boolean            wasAliased, isFolder;
  37.             
  38.     if (level >= kMaxRecursion)
  39.         return;
  40.             
  41.     pBlock.hFileInfo.ioVRefNum = folder -> vRefNum;
  42.     pBlock.hFileInfo.ioFDirIndex = 0;
  43.     pBlock.hFileInfo.ioNamePtr = folder -> name;
  44.     pBlock.hFileInfo.ioDirID = folder -> parID;
  45.     
  46.     error = PBGetCatInfoSync (&pBlock);
  47.     
  48.     if (error)
  49.         return;
  50.  
  51.     dirID = pBlock.hFileInfo.ioDirID;
  52.     pBlock.hFileInfo.ioFDirIndex = 1;
  53.     pBlock.hFileInfo.ioNamePtr = name;
  54.     
  55.     while (!error) {
  56.         
  57.         name [0] = '\0';
  58.         
  59.         pBlock.hFileInfo.ioDirID = dirID;
  60.         error = PBGetCatInfoSync (&pBlock);
  61.         
  62.         if (!error) {
  63.             
  64.             FSMakeFSSpec (pBlock.hFileInfo.ioVRefNum, dirID, name, &spec);
  65.             
  66.             if (pBlock.hFileInfo.ioFlAttrib & 16)
  67.                 isFolder = true;
  68.             else 
  69.                 error = ResolveAliasFile (&spec, true, &isFolder, &wasAliased);
  70.             
  71.             if (error == noErr) {
  72.                 if (isFolder)
  73.                     openAllInFolder (&spec, level + 1);
  74.                 else
  75.                     FSpOpenResFile (&spec, fsRdPerm);
  76.             }
  77.         }
  78.         ++ pBlock.hFileInfo.ioFDirIndex;
  79.     }
  80. }
  81.  
  82.  
  83. OSErr getMyPrefsFolder (FSSpec *myPrefs)
  84.  
  85. {
  86.     OSErr        error;
  87.     long        newDirID;
  88.     long        prefsDir;
  89.     short        prefsVol;
  90.     Boolean        wasAliased, isFolder;
  91.     
  92.     error = FindFolder (kOnSystemDisk, kPreferencesFolderType, true, &prefsVol,
  93.                 &prefsDir);
  94.     
  95.     if (error)
  96.         return (error);
  97.     
  98.     error = FSMakeFSSpec (prefsVol, prefsDir, kFolderName, myPrefs);
  99.     
  100.     if (error) {        // Our prefs folder does not exist.
  101.     
  102.         error = FSpDirCreate (myPrefs, smRoman, &newDirID);
  103.         
  104.         if (error)
  105.             return (error);
  106.             
  107.         error = FSMakeFSSpec (prefsVol, prefsDir, kFolderName, myPrefs);
  108.  
  109.         return (error);        
  110.     }
  111.             
  112.     error = ResolveAliasFile (myPrefs, true, &isFolder, &wasAliased);
  113.     if (isFolder == false)    // we only want folders!
  114.         error = fnfErr;
  115.         
  116.     return (error);
  117. }
  118.  
  119. void myPatch (Str255 fileName)
  120.  
  121. {
  122.     OSErr                    error;
  123.     FSSpec                    mySpec;
  124.     ProcessInfoRec            pInfo;
  125.     ProcessSerialNumber        psn;
  126.     static Boolean            blockRecursion = false;
  127.     long                    oldA4 = SetCurrentA4 ();
  128.     
  129.     if (blockRecursion) {            // Don't open any files, we've already taken care of it
  130.         SetA4 (oldA4);
  131.         return;
  132.     } else
  133.         blockRecursion = true;        // Prevent recursive calls to HOpenResFile
  134.         
  135.     // Are we opening the ResEdit Prefs file?
  136.     if (!EqualString ("\pResEdit Preferences", fileName, true, true))
  137.         goto Exit;
  138.         
  139.     // And is this ResEdit?
  140.     GetCurrentProcess (&psn);
  141.     pInfo.processInfoLength = sizeof (pInfo);
  142.     pInfo.processName = nil;
  143.     pInfo.processAppSpec = nil;
  144.     GetProcessInformation (&psn, &pInfo);
  145.     
  146.     if (pInfo.processSignature != 'RSED')    // Not ResEdit, bail out
  147.         goto Exit;
  148.         
  149.     // Get FSSpec to the "More Preferences" folder
  150.     error = getMyPrefsFolder (&mySpec);
  151.     if (!error)
  152.         openAllInFolder (&mySpec, 0);        // Open prefs files contained
  153.         
  154. Exit:
  155.  
  156.     blockRecursion = false;                    // we're done
  157.     SetA4 (oldA4);
  158.     
  159.     return;
  160. }
  161.  
  162. void asm patchEntry (void)
  163. {
  164.     move.l    6(sp), -(sp)                // re-push file name parameter
  165.     jsr        myPatch                        // call myPatch
  166.     addq.l    #4, sp                        // pop parameter
  167.     clr.l    -(sp)                        // reserve four bytes
  168.     movem.l    a0-a1/d0-d2/a4, -(sp)        // preserve volatile regs
  169.     jsr        SetCurrentA4                // setup A4 world
  170.     move.l    addrStorage, 0x18(sp)        // put ROM address in previously reserved stack space
  171.     movem.l    (sp)+, a0-a1/d0-d2/a4        // restore regs (a4 included)
  172.     rts                                    // jump to ROM by "returning" to the ROM address
  173. }
  174.  
  175. void main (void)
  176. {
  177.     long        oldA4 = SetCurrentA4 ();
  178.     Handle        myself;
  179.     long        response;
  180.     Boolean        ok = false;
  181.     FSSpec        spec;
  182.     
  183.     myself = Get1IndResource ('INIT', 1);
  184.     if (!myself)
  185.         goto Exit;
  186.         
  187.     if (Button ())
  188.         goto Exit;
  189.     
  190.     Gestalt (gestaltSystemVersion, &response);
  191.     if (response < 0x00000700)
  192.         goto Exit;
  193.     
  194.     ok = true;
  195.     DetachResource (myself);
  196.  
  197.     addrStorage = (long) GetToolTrapAddress (0xA81A);
  198.     SetToolTrapAddress ((UniversalProcPtr) patchEntry, 0xA81A);
  199.     
  200.     getMyPrefsFolder (&spec);        // create the prefs folder if need be
  201.     
  202. Exit:
  203.     
  204.     if (ok)
  205.         ShowInit (kGoodIcon, kStandardDisplacemet);
  206.     else
  207.         ShowInit (kBadIcon, kStandardDisplacemet);
  208.  
  209.     SetA4 (oldA4);
  210. }